package cn.clq.ymcc.web.controller;

import cn.clq.ymcc.service.IKillActivityService;
import cn.clq.ymcc.domain.KillActivity;
import cn.clq.ymcc.query.KillActivityQuery;
import cn.clq.ymcc.result.JSONResult;
import cn.clq.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/killActivity")
public class KillActivityController {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:ss");

    @Autowired
    public IKillActivityService killActivityService;


    /**
     * @description: 发布秒杀活动
     * @param: activityId
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/8/6 1:36
     */
    @GetMapping("/publish/{activityId}")
    public JSONResult publish(@PathVariable("activityId")Long activityId){
        killActivityService.publish(activityId);
        return JSONResult.success();
    }


    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JSONResult saveOrUpdate(@RequestBody KillActivity killActivity){
        //设置活动时间段
        killActivity.setTimeStr(simpleDateFormat.format(killActivity.getStartTime()));
        if(killActivity.getId()!=null){
            killActivityService.updateById(killActivity);
        }else{
            killActivityService.insert(killActivity);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        killActivityService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killActivityService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JSONResult list(){
        return JSONResult.success(killActivityService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JSONResult page(@RequestBody KillActivityQuery query){
        Page<KillActivity> page = new Page<KillActivity>(query.getPage(),query.getRows());
        page = killActivityService.selectPage(page);
        return JSONResult.success(new PageList<KillActivity>(page.getTotal(),page.getRecords()));
    }
}
